package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.*;
import cn.gson.fengboot.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional(rollbackFor = Exception.class)
public class TfyClientService {

    @Autowired
    TfyclueFormDao tfyclueFormDao;//线索

    @Autowired
    TfyclientFormDao tfyclientFormDao;//客户

    @Autowired
    TfylinkmanFormDao tfylinkmanFormDao;//联系人

    @Autowired
    DbusinessDao dbusinessDao;//商机dao

    @Autowired
    ZcontractDao zcontractDao;//合同dao

    @Autowired
    ZorderDao zorderDao;//订单dao

    @Autowired
    ZreturnedmoneyDao zreturnedmoneyDao;//回款计划dao

    @Autowired
    ZreturnedgoodDao zreturnedgoodDao;//退货dao

    @Autowired
    ZbarterDao zbarterDao;//换货dao

    @Autowired
    ZdeliveryDao zdeliveryDao;//交付计划dao

    @Autowired
    ZchukuDao zchukuDao;//出库dao

    @Autowired
    ZdeliverylogDao zdeliverylogDao;//交付记录dao

    @Autowired
    ZinvoiceDao zinvoiceDao;//发票dao

    @Autowired
    ZreturnedmoneylogDao zreturnedmoneylogDao;//回款记录dao

    @Autowired
    ZrukuDao zrukuDao;//入库dao

    @Autowired
    LkefuDao lkefuDao;//客户服务dao

    @Autowired
    LtousuDao ltousuDao;//客户投诉dao

    @Autowired
    LwxzbDao lwxzbDao;//维修工单dao

    @Autowired
    TfyrelationwarnDao tfyrelationwarnDao;//联系提醒dao

    @Autowired
    TfylinkmanMapper tfylinkmanMapper;//联系人mapper

    @Autowired
    TfyclientMapper tfyclientMapper;//客户mapper

    @Autowired
    ZdeliveryMapper zdeliveryMapper;//交付计划mapper

    @Autowired
    DbusinessMapper dbusinessMapper;//销售机会mapper

    @Autowired
    ZcontractMapper zcontractMapper;//合同mapper

    @Autowired
    ZorderMapper zorderMapper;//订单mapper

    @Autowired
    ZreturnedmoneyMapper zreturnedmoneyMapper;//回款计划mapper

    @Autowired
    ZreturnedgoodMapper zreturnedgoodMapper;//退货mapper

    @Autowired
    ZbarterMapper zbarterMapper;//换货mapper

    @Autowired
    ZchukuMapper zchukuMapper;//出库mapper

    @Autowired
    ZdeliverylogMapper zdeliverylogMapper;//交付记录mapper

    @Autowired
    ZinvoiceMapper zinvoiceMapper;//发票mapper

    @Autowired
    ZreturnedmoneylogMapper zreturnedmoneylogMapper;//回款记录mapper

    @Autowired
    ZrukuMapper zrukuMapper;//入库mapper

    @Autowired
    LkefuMapper lkefuMapper;//客户服务mapper

    @Autowired
    LtousuMapper ltousuMapper;//客户投诉mapper

    @Autowired
    LwxzbMapper lwxzbMapper;//维修工单mapper

    @Autowired
    TfyrelationwarnMapper tfyrelationwarnMapper;//联系提醒mapper

    //客户、模糊查询
    public List<TfyclientForm> findAllClient(String search,String search1){
        return tfyclientMapper.findAllClient(search,search1);
    }

    //根据员工id查询客户、模糊查询
    public List<TfyclientForm> findAllygClient(String search,Integer empId){
        return tfyclientMapper.findAllygClient(search,empId);
    }

    //已关注客户、模糊查询
    public List<TfyclientForm> findAllClientgz(String search){
        return tfyclientMapper.findAllClientgz(search);
    }

    //已失效客户、模糊查询
    public List<TfyclientForm> findAllClientzt(String search){
        return tfyclientMapper.findAllClientzt(search);
    }

    //客户id查询
    public TfyclientForm findIdClient(Integer clientId){
        return tfyclientMapper.findIdClient(clientId);
    }

    //今日需联系客户、时间查询
    public List<TfyclientForm> findClientDate(){
        return tfyclientMapper.findClientDate();
    }

    //分配给我的客户查询
    public List<TfyclientForm> findClient(String search){
        return tfyclientMapper.findClient(search);
    }

    //客户新增,修改方法
    public Integer addclient(TfyclientForm kh,String mc) {
        int clientId=0;
        Integer tgz = tfyclientMapper.cxclientname(kh.getClientName());

        if(tgz== 0 && kh.getClientId()==0){
            kh.setClientState(1);
            kh.setClientKhzt("潜在客户");
            kh.setClientIffollowup(0);
            if(kh.getClientId() == 0){
                kh.setClientPayattention("加关注");
            }
            TfyclientForm save = tfyclientFormDao.save(kh);
            System.out.println("save:"+save);
            clientId= save.getClientId();
        }

        if(kh.getClientName().equals(mc)){
            TfyclientForm save = tfyclientFormDao.save(kh);
            clientId= save.getClientId();
        }else{
            if(tgz== 0 && kh.getClientId()!=0){
                TfyclientForm save = tfyclientFormDao.save(kh);
                System.out.println("save:"+save);
                clientId= save.getClientId();
            }
        }
        return clientId;
    }

    //客户er新增,修改方法
    public Integer addclienter(TfyclientForm kh) {
        int clientId=0;
        Integer tgz = tfyclientMapper.cxclientname(kh.getClientName());
        if(tgz== 0 && kh.getClientId()==0){
            kh.setClientState(1);
            kh.setClientKhzt("潜在客户");
            kh.setClientIffollowup(0);
            if(kh.getClientId() == 0){
                kh.setClientPayattention("加关注");
            }
            TfyclientForm save = tfyclientFormDao.save(kh);
            System.out.println("save:"+save);
            clientId= save.getClientId();
        }
        return clientId;
    }

    //新增默认客户联系人方法
    public void addclientlxr(Integer khid) {
        TfyclientForm kh = tfyclientFormDao.findById(khid).get();
        TfylinkmanForm lxr = new TfylinkmanForm();
        lxr.setLinkmanName(kh.getClientName());
        lxr.setLinkmanMobile(kh.getClientMobile());
        lxr.setLinkmanPhone(kh.getClientPhone());
        lxr.setLinkmanSite(kh.getClientSite());
        lxr.setLinkmanSz(kh.getClientSz());
        lxr.setLinkmanState(1);
        lxr.setEmpId(kh.getEmpId());
        lxr.setClientId(kh);
        tfylinkmanFormDao.save(lxr);
    }

    //客户物理删除
    public void deleteClient(String khdx){
        JSONObject obj = (JSONObject)JSONObject.parse(khdx);
        List khdxlist = obj.getObject("khdxlist", List.class);
        //转化成JSONArray
        JSONObject object=null;
        JSONArray json = JSONArray.parseArray(String.valueOf(khdxlist));
        System.out.println("jihe：："+khdxlist.toString());
        for (int i=0; i<json.size();i++) {
            object = (JSONObject) json.get(i);//得到对象
            Integer khdxs = object.getInteger("clientId");
            System.out.println("客户ID：" + khdxs);
            System.out.println("siowjj " + obj.toString());
            TfyclientForm tfyclientForm = tfyclientFormDao.findById(khdxs).get();
            System.out.println("客户：" + tfyclientForm.toString());
            tfyclientFormDao.delete(tfyclientForm);
            //联系人
            List<TfylinkmanForm> tfylinkmanForm = tfylinkmanMapper.findAllkhlxr(khdxs);
            for (int j = 0; j <= tfylinkmanForm.size()-1; j++) {
                tfylinkmanFormDao.delete(tfylinkmanForm.get(j));
                System.out.println("zt:"+tfylinkmanForm.get(j).getLinkmanState());
            }
        }
    }

    //单行，批量逻辑删除客户对象
    public Integer khsc(String khdx) {
        JSONObject obj = (JSONObject)JSONObject.parse(khdx);
        List khdxlist = obj.getObject("khdxlist", List.class);
        //转化成TSONArray
        JSONObject object=null;
        JSONArray json = JSONArray.parseArray(String.valueOf(khdxlist)) ;
        System.out.println("jihe：："+khdxlist.toString());
        int abb = 0;
        for (int i=0; i<json.size();i++) {
            object = (JSONObject)json.get(i);//得到对象
            Integer khdxs =object.getInteger("clientId");
            System.out.println("客户ID："+khdxs);
            System.out.println("siowjj "+obj.toString());
            TfyclientForm tfyclientForm = tfyclientFormDao.findById(khdxs).get();
            //商机
            System.out.println(tfyclientMapper.findclientsj(khdxs).size());
            if(tfyclientMapper.findclientsj(khdxs).size() == 0){
                tfyclientForm.setClientState(0);
                TfyclientForm save = tfyclientFormDao.save(tfyclientForm);
                System.out.println("khsave:"+save);
                abb = 1;
                //联系人
                List<TfylinkmanForm> tfylinkmanForm = tfylinkmanMapper.findAllkhlxr(khdxs);
                for (int j = 0; j <= tfylinkmanForm.size()-1; j++) {
                    tfylinkmanForm.get(j).setLinkmanState(0);
                    tfylinkmanFormDao.save(tfylinkmanForm.get(j));
                    System.out.println("zt:"+tfylinkmanForm.get(j).getLinkmanState());
                }
            }
        }
        System.out.println("客户："+abb);
        return abb;
    }

    //单行，批量恢复客户状态
    public void khhf(String khdx) {
        JSONObject obj = (JSONObject)JSONObject.parse(khdx);
        List khdxlist = obj.getObject("khdxlist", List.class);
        //转化成TSONArray
        JSONObject object=null;
        JSONArray json = JSONArray.parseArray(String.valueOf(khdxlist)) ;
        System.out.println("jihe：："+khdxlist.toString());
        for (int i=0; i<json.size();i++) {
            object = (JSONObject)json.get(i);//得到对象
            Integer khdxs =object.getInteger("clientId");
            System.out.println("客户ID："+khdxs);
            System.out.println("siowjj "+obj.toString());
            TfyclientForm tfyclientForm = tfyclientFormDao.findById(khdxs).get();
            System.out.println("客户："+tfyclientForm.toString());
            tfyclientForm.setClientState(1);//客户状态1
            TfyclientForm save = tfyclientFormDao.save(tfyclientForm);

            //联系人
            List<TfylinkmanForm> tfylinkmanForm = tfylinkmanMapper.findAllkhlxr(khdxs);
            for (int j = 0; j <= tfylinkmanForm.size()-1; j++) {
                tfylinkmanForm.get(j).setLinkmanState(1);//联系人状态1
                tfylinkmanFormDao.save(tfylinkmanForm.get(j));
                System.out.println("zt:"+tfylinkmanForm.get(j).getLinkmanState());
            }
        }
    }

    //客户id查询跟进记录
    public List<Dtail> selectAllTails(Integer clientId) {
        return tfyclientMapper.selectAllTails(clientId);
    }

    //客户id查询商机信息
    public List<Dbusiness> findclientsj(Integer clientId){
        return tfyclientMapper.findclientsj(clientId);
    }

    //客户id or id查询商机信息
    public List<Dbusiness> findclientsjtwo(Integer clientId,Integer clientId2){
        return tfyclientMapper.findclientsjtwo(clientId,clientId2);
    }

    //修改客户跟进状态
    public void gjztClient(String khdx) {
        JSONObject obj = (JSONObject)JSONObject.parse(khdx);
        List khdxlist = obj.getObject("khdxlist", List.class);
        //转化成TSONArray
        JSONObject object=null;
        JSONArray json = JSONArray.parseArray(String.valueOf(khdxlist));
        System.out.println("jihe：："+khdxlist.toString());
        for (int i=0; i<json.size();i++) {
            object = (JSONObject)json.get(i);//得到对象
            Integer khdxs =object.getInteger("clientId");
            System.out.println("客户ID："+khdxs);
            System.out.println("siowjj "+obj.toString());
            TfyclientForm tfyclientForm = tfyclientFormDao.findById(khdxs).get();
            System.out.println("客户："+tfyclientForm.toString());
            tfyclientForm.setClientIffollowup(1);
            tfyclientFormDao.save(tfyclientForm);
        }
    }

    //单行，批量修改关注状态  -->已关注
    public void jgzclient(String khdx){
        JSONObject obj = (JSONObject)JSONObject.parse(khdx);
        List khdxlist = obj.getObject("khdxlist", List.class);
        //转化成TSONArray
        JSONObject object=null;
        JSONArray json = JSONArray.parseArray(String.valueOf(khdxlist)) ;
        System.out.println("jihe：："+khdxlist.toString());
        for (int i=0; i<json.size();i++) {
            object = (JSONObject)json.get(i);//得到对象
            Integer khdxs =object.getInteger("clientId");
            System.out.println("客户ID："+khdxs);
            System.out.println("siowjj "+obj.toString());
            TfyclientForm tfyclientForm = tfyclientFormDao.findById(khdxs).get();
            System.out.println("客户："+tfyclientForm.toString());
            tfyclientForm.setClientPayattention("已关注");
            TfyclientForm save = tfyclientFormDao.save(tfyclientForm);
        }
    }

    //单行，批量修改关注状态  -->加关注
    public void ygzclient(String khdx){
        JSONObject obj = (JSONObject)JSONObject.parse(khdx);
        List khdxlist = obj.getObject("khdxlist", List.class);
        //转化成TSONArray
        JSONObject object=null;
        JSONArray json = JSONArray.parseArray(String.valueOf(khdxlist)) ;
        System.out.println("jihe：："+khdxlist.toString());
        for (int i=0; i<json.size();i++) {
            object = (JSONObject)json.get(i);//得到对象
            Integer khdxs =object.getInteger("clientId");
            System.out.println("客户ID："+khdxs);
            System.out.println("siowjj "+obj.toString());
            TfyclientForm tfyclientForm = tfyclientFormDao.findById(khdxs).get();
            System.out.println("客户："+tfyclientForm.toString());
            tfyclientForm.setClientPayattention("加关注");
            TfyclientForm save = tfyclientFormDao.save(tfyclientForm);
        }
    }

    //查询员工和对应部门
    public List<QempFrom> findemp(String search){
        return tfyclientMapper.findemp(search);
    }

    //根据员工id查询员工信息
    public QempFrom cxidemp(Integer empId){
        return tfyclientMapper.cxidemp(empId);
    }

    //<!--根据导入负责人名称查询员工名称-->
    public QempFrom cxempname(String empName){
        return tfyclientMapper.cxempname(empName);
    }

    //查询客户名称是否相同
    public Integer cxclientname(String clientName){
        return tfyclientMapper.cxclientname(clientName);
    }

    //客户地图报表
    public List<TfyclientForm> findditu(){
        return tfyclientMapper.findditu();
    }

    //客户饼状图报表 客户行业
    public List<TfyclientForm> findbingz(){
        return tfyclientMapper.findbingz();
    }

    //客户饼状图报表 客户来源
    public List<TfyclientForm> findbingzer(){
        return tfyclientMapper.findbingzer();
    }

    //客户饼状图报表 客户级别
    public List<TfyclientForm> findbingzsan(){
        return tfyclientMapper.findbingzsan();
    }

    //客户饼状图报表 客户类型
    public List<TfyclientForm> findbingzsi(){
        return tfyclientMapper.findbingzsi();
    }

    //柱状图
    public List<TfyclientForm> findzhuz(){
        return tfyclientMapper.findzhuz();
    }

    //客户全部视图
    public List<Tfyselectvo> allselect(Integer clientId){
        return tfyclientMapper.allselect(clientId);
    }

    //交付计划z
    public List<Zdelivery> jfjh(Integer id){
        return zdeliveryMapper.jfjh(id);
    }

    //<!--谭方圆=>客户视图的查询合同详情-->
    public List<Zcontract> hetonglist(Integer id){
        return zcontractMapper.hetonglist(id);
    }

//    <!--谭方圆=>跳订单详情查询-->
    public List<Zorder> ddlists(Integer id){
        return zorderMapper.ddlists(id);
    }

    //<!--谭方圆=>客户查询回款计划详情-->
    public List<Zreturnedmoney> huikuanlists(Integer id){
        return zreturnedmoneyMapper.huikuanlists(id);
    }

    //<!--谭方圆=>跳退货查询详情-->
    public List<Zreturnedgood> tuihuolists(Integer id){
        return zreturnedgoodMapper.tuihuolists(id);
    }

    //<!--谭方圆=>跳换货查询详情-->
    public List<Zbarter> huanhuolists(Integer id){
        return zbarterMapper.huanhuolists(id);
    }

    //客户合并
    public void hebinglist(String busform){
        System.out.println("kh:"+busform);
        JSONObject obj = (JSONObject)JSONObject.parse(busform);
        TfyclientForm khdxlist = obj.getObject("khdxlist", TfyclientForm.class);
        Integer id1 = obj.getObject("id1", Integer.class);
        Integer id2 = obj.getObject("id2", Integer.class);
        //新增新客户
        TfyclientForm save = tfyclientFormDao.save(khdxlist);
        System.out.println("我来了");
        System.out.println(save.toString());
        //联系人合并1
        List<TfylinkmanForm> tfylinkmanForms = tfylinkmanMapper.findAllkhcxlxrid(id1,id2);
        tfylinkmanForms.forEach(b->{
            b.setClientId(save);
        });
        tfylinkmanFormDao.saveAll(tfylinkmanForms);
        //商机合并2
        List<Dbusiness> dbusinesses = tfyclientMapper.findclientsjtwo(id1,id2);
        dbusinesses.forEach(a->{
            a.setClient(save);
            System.out.println(a);
        });
        dbusinessDao.saveAll(dbusinesses);
        //合同合并3
        List<Zcontract> zcontracts = zcontractMapper.tfyhetonlist(id1,id2);
        zcontracts.forEach(c->{
            c.setClientId(save);
        });
        zcontractDao.saveAll(zcontracts);
        //订单合并4
        List<Zorder> zorders = zorderMapper.tfyddlists(id1,id2);
        zorders.forEach(d->{
            d.setClientId(save);
        });
        zorderDao.saveAll(zorders);
        //回款计划合并5
        List<Zreturnedmoney> zreturnedmonies = zreturnedmoneyMapper.tfyhuikuanlists(id1,id2);
        zreturnedmonies.forEach(e->{
            e.setClientId(save);
        });
        zreturnedmoneyDao.saveAll(zreturnedmonies);
        //退货合并6
        List<Zreturnedgood> zreturnedgoods = zreturnedgoodMapper.tfytuihuolists(id1,id2);
        zreturnedgoods.forEach(f->{
            f.setClientId(save);
        });
        zreturnedgoodDao.saveAll(zreturnedgoods);
        //换货合并7
        List<Zbarter> zbarters = zbarterMapper.tfyhuanhuolists(id1,id2);
        zbarters.forEach(f->{
            f.setClientId(save);
        });
        zbarterDao.saveAll(zbarters);
        //交付计划合并8
        List<Zdelivery> zdeliveries = zdeliveryMapper.tfyjfjh(id1,id2);
        zdeliveries.forEach(f->{
            f.setClientId(save);
        });
        zdeliveryDao.saveAll(zdeliveries);
        //出库合并9
        List<Zchuku> zchukus = zchukuMapper.tfychukuliebiao(id1,id2);
        zchukus.forEach(f->{
            f.setClientId(save);
        });
        zchukuDao.saveAll(zchukus);
        //交付记录合并10
        List<Zdeliverylog> zdeliverylogs = zdeliverylogMapper.tfykaipiao(id1,id2);
        zdeliverylogs.forEach(f->{
            f.setClientId(save);
        });
        zdeliverylogDao.saveAll(zdeliverylogs);
        //发票合并11
        List<Zinvoice> zinvoices = zinvoiceMapper.tfyfapiao(id1,id2);
        zinvoices.forEach(f->{
            f.setClientId(save);
        });
        zinvoiceDao.saveAll(zinvoices);
        //回款记录合并12
        List<Zreturnedmoneylog> zreturnedmoneylogs = zreturnedmoneylogMapper.tfyhueikuanjl(id1,id2);
        zreturnedmoneylogs.forEach(f->{
            f.setClientId(save);
        });
        zreturnedmoneylogDao.saveAll(zreturnedmoneylogs);
        //入库合并13
        List<Zruku> tfyruku = zrukuMapper.tfyruku(id1,id2);
        tfyruku.forEach(f->{
            f.setClientId(save);
        });
        zrukuDao.saveAll(tfyruku);
        //客户服务合并14
        List<Lkefu> tfyfuwu = lkefuMapper.tfyfuwu(id1,id2);
        tfyfuwu.forEach(f->{
            f.setClientId(save);
        });
        lkefuDao.saveAll(tfyfuwu);
        //客户投诉合并15
        List<Ltousu> ltousus = ltousuMapper.tfytousu(id1,id2);
        ltousus.forEach(f->{
            f.setClientId(save);
        });
        ltousuDao.saveAll(ltousus);
        //维修工单合并16
        List<Lwxzb> lwxzbs = lwxzbMapper.tfyweixiu(id1,id2);
        lwxzbs.forEach(f->{
            f.setClientId(save);
        });
        lwxzbDao.saveAll(lwxzbs);
        //联系提醒合并17
        List<TfyrelationwarnFrom> tfyrelationwarnFroms = tfyrelationwarnMapper.khhbrelationwarn(id1,id2);
        tfyrelationwarnFroms.forEach(f->{
            f.setClientId(save);
        });
        tfyrelationwarnDao.saveAll(tfyrelationwarnFroms);
        //删除原客户
        tfyclientFormDao.deleteById(id1);
        tfyclientFormDao.deleteById(id2);
    }

}
